<script setup lang="ts">
import { reqNoticeList, reqAddNotice, reqDeleteNotice, reqUpdateNotice } from '@/api/notice'
import FormDrawer from '@/components/FormDrawer.vue'
import { useInitTable, useInitForm } from '@/composables/useCommon'
const { tableData, loading, currentPage, pageSize, total, getData, handleDelete } = useInitTable({
  getList: reqNoticeList,
  delete: reqDeleteNotice
})

const { formDrawerRef, formRef, drawerTitle, form, rules, handleSubmit, handleAdd, handleEdit } = useInitForm({
  getData,
  initialForm: { title: '', content: '' },
  rules: {
    title: [{ required: true, message: '请输入公告标题', trigger: 'blur' }],
    content: [{ required: true, message: '请输入公告内容', trigger: 'blur' }]
  },
  update: reqUpdateNotice,
  create: reqAddNotice
})
</script>

<template>
  <el-card shadow="never" style="border: 0; margin: 10px">
    <ListHeader @create="handleAdd" @refresh="getData" />
    <el-divider />
    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
      <el-table-column label="公告标题" prop="title" width="120" align="center"></el-table-column>
      <el-table-column label="公共内容" prop="content" align="center"></el-table-column>
      <el-table-column label="发布时间" prop="create_time" width="380" align="center"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template #default="{ row }">
          <el-button size="small" type="primary" icon="edit" @click="handleEdit(row)">编辑</el-button>
          <el-popconfirm title="是否确认要删除吗?" width @confirm="handleDelete(row.id)">
            <template #reference>
              <el-button size="small" type="danger" icon="delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next, total"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        @current-change="getData"
      ></el-pagination>
    </div>
    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="公告内容" prop="content">
          <el-input type="textarea" :rows="5" v-model="form.content"></el-input>
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>

<style lang="scss" scoped>
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
}
</style>
